package com.benew.ntt.ydeval.view

import android.os.Handler
import android.os.Looper
import android.os.Message
import com.benew.ntt.evaluate.sdk.EvalSDKEntity
import com.ntt.core.nlogger.NLogger

class EvalHandler(looper: Looper, callback: Callback) : Handler(looper, callback) {

    var mAutoClose = false

    fun sendBeforeOrigin(entity: EvalSDKEntity) {
        val msg = Message.obtain()
        msg.what = EvalWhat.BEFORE_ORIGIN
        msg.obj = entity
        sendMessage(msg)
    }


    fun sendPlayRecord() {
        sendStopCloseCountDown()
        sendEmptyMessage(EvalWhat.PLAY_RECORD)
    }

    fun sendPlayOriginal(notStarted: Boolean) {
        sendStopCloseCountDown()
        val msg = Message.obtain()
        msg.what = EvalWhat.PLAY_ORIGINAL
        msg.arg1 = if (notStarted) 1 else 0
        sendMessage(msg)
    }

    fun sendStopPlayOriginal(notStarted: Boolean) {
        val msg = Message.obtain()
        msg.what = EvalWhat.STOP_PLAY_ORIGINAL
        msg.arg1 = if (notStarted) 1 else 0
        sendMessage(msg)
        sendCloseCountDown()
    }

    fun sendStopPlayRecord() {
        sendEmptyMessage(EvalWhat.STOP_PLAY_RECORD)

        sendCloseCountDown()
    }


    /**
     * 发送关闭倒计时
     */
    fun sendCloseCountDown() {
        removeMessages(EvalWhat.CLOSE_COUNTDOWN)
        if (mAutoClose) {
            val msg = Message.obtain()
            msg.what = EvalWhat.CLOSE_COUNTDOWN
            sendMessageDelayed(msg, 4000)
        }
    }


    /**
     * 停止倒计时
     */
    fun sendStopCloseCountDown() {
        NLogger.d("取消自动关闭")
        removeMessages(EvalWhat.CLOSE_COUNTDOWN)
    }

}